#include"Stack.h"
void SKInit(Stack* ps) {
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void SKDestroy(Stack* ps) {
	assert(ps);
	if (ps->arr) {
		free(ps->arr);
		ps->arr = NULL;
	}
	ps->capacity = ps->top = 0;
}
bool SKIsEmpty(Stack* ps) {
	assert(ps);
	return ps->top == 0;
}


void SKPush(Stack* ps, SKDataType val) {
	assert(ps);
	if (ps->capacity == ps->top) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; 
		SKDataType* tmp = (SKDataType*)realloc(ps->arr, sizeof(SKDataType) * newCapacity);
		if (tmp == NULL) {
			perror("realloc fail!!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	ps->arr[ps->top++] = val;
}
void SKPop(Stack* ps) {
	assert(ps);
	assert(!SKIsEmpty(ps));
	ps->top--;
}
SKDataType SKTop(Stack* ps) {
	assert(ps);
	assert(!SKIsEmpty(ps));
	return ps->arr[ps->top - 1];
}
int SKSize(Stack* ps) {
	assert(ps);
	return ps->top;
}
